using System;
using System.Data;
using System.Web;

using vs.Core;

namespace Forum
{
    public partial class member_profile : System.Web.UI.UserControl
    {
        private void RegisterScriptIMG()
        {
            string js = MemberController.jsImage(txtImageUrl.ClientID);
            Page.ClientScript.RegisterClientScriptBlock(typeof(string), "setvalue", js);
            string popup = ResolveUrl("~/desktops/forum/avatar.aspx");
            lnkImage.NavigateUrl = "javascript:popup2('" + popup + "', 500, 400);";
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!Page.IsPostBack)
                {
                    if (!Request.IsAuthenticated)
                    {
                        string url = UrlProvider.AdminUrl("user", "login");
                        Response.Redirect(url);
                        return;
                    }

                    string username = HttpContext.Current.User.Identity.Name;
                    lblUserName.Text = username;

                    MemberController db = new MemberController();
                    MemberInfo mem = db.Load(username);
                    if (mem != null)
                    {
                        lblPosts.Text = mem.Posts.ToString();
                        txtSlogan.Text = mem.Slogan;
                        txtImageUrl.Text = mem.ImageUrl;
                        txtSign.Text = mem.Sign;

                        db.Update(mem);
                    }
                    else
                    {
                        mem = new MemberInfo();
                        mem.UserName = username;
                        db.Insert(mem);
                    }

                    RegisterScriptIMG();
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                MemberInfo mem = new MemberInfo();
                mem.UserName = lblUserName.Text;
                mem.ImageUrl = txtImageUrl.Text;
                mem.Slogan = txtSlogan.Text;
                mem.Sign = txtSign.Text.Trim();

                MemberController db = new MemberController();
                db.Update(mem);

                Response.Redirect("/");
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }

        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("/");
        }
    }
}